package com.app.view.component.room.seat 
{
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.events.MouseEvent;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.geom.Rectangle;
	import flash.events.TextEvent;
	
	import org.component.Button;
	import com.peachcastle.events.SeatEvent;
	
	import com.peachcastle.core.Display;
	import com.peachcastle.tools.Utils;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class PlayrOperate extends Display
	{
		private var _seatid:int = 0;
		
		private var _chips:int = 0;
		
		private var _barFirst:Point = null;
		
		private var _btnBasePour:Button = null;
		private var _btnCallPour:Button = null;
		private var _btnGiveup:Button = null;
		
		private var _txtPour:Button = null;
		
		private var _btnChips1:Button = null;
		private var _btnChips2:Button = null;
		private var _btnChips5:Button = null;
		private var _btnChips10:Button = null;
		private var _btnChips50:Button = null;
		
		private var _connection:Array = ["1", "2", "5", "10", "50"];

		private var _bar:MovieClip = null;
		private var _scrollBarWidth:int = 115;
		private var _isHeapChips:Boolean = true;
		
		private var _minPour:int = 0;
		private var _maxPour:int = 10;
		
		public function PlayrOperate(ui:MovieClip, seatid:int) 
		{
			_seatid = seatid;
			super(ui, true);
		}
		
		override protected function initialization():void 
		{
			super.initialization();
			
			_btnBasePour = new Button(assest["btn_base_pour"]);
			_btnCallPour = new Button(assest["btn_call_pour"]);
			_btnGiveup = new Button(assest["btn_give_up"]);
			_bar = assest['bar']['bar'];
			_barFirst = new Point(_bar.x, _bar.y);
			
			for each(var str:String in _connection)
			{
				this["_btnChips" + str] = new Button(assest["btn_chips_" + str]);
			}

			setOperation('init');
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();
		}
		
		private function initBarPosition():void
		{
			_bar.x = _barFirst.x;
			_bar.y = _barFirst.y;
		}
		
		private function setBarPosition(chips:int):void
		{
			var tmp:Number = (chips / _maxPour) * _scrollBarWidth;
			if (tmp < _barFirst.x) _bar.x = _barFirst.x;
		}
		
		private function setBarDrag(b:Boolean):void
		{
			if (b)
			{
				_bar.buttonMode = true;
				_bar.useHandCursor = true;
				_bar.addEventListener(MouseEvent.MOUSE_DOWN, onDrag);
			}else
			{
				_bar.removeEventListener(MouseEvent.MOUSE_DOWN, onDrag);
			}
		}
		
		private function onDrag(e:MouseEvent):void
		{	
			assest.stage.addEventListener(MouseEvent.MOUSE_UP, onDrop);
			assest.stage.addEventListener(MouseEvent.MOUSE_MOVE, onSlide);

			_bar.startDrag(false, new Rectangle(_barFirst.x, _barFirst.y, _scrollBarWidth, 0));	//位置等于是写死了
		}
		
		private function onDrop(e:MouseEvent):void
		{
			assest.stage.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
			assest.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onSlide);
			_bar.stopDrag();	
		}
		
		private function onSlide(e:MouseEvent):void
		{
			_isHeapChips = false;

			if (_bar.x < _barFirst.x) _bar.x = _barFirst.x;
			
			var c:int = Math.ceil(((_bar.x - _barFirst.x) / _scrollBarWidth) * _maxPour);
			if (c < _minPour) c = _minPour;
			_txtPour.setLabel(String(c));
		}
		
		public function setOperation(act:String, status:Boolean = false, minPour:int = -1, maxPour:int = -1):void
		{	
			if (maxPour >= 0) _maxPour = maxPour;
			
			if (status)
			{
				showAssest();
				initBarPosition();
				
				for each(var str1:String in _connection)
				{
					if (_maxPour < int(str1))
					{
						Button(this["_btnChips" + str1]).setDisable(true);
					}else
					{
						Button(this["_btnChips" + str1]).setDisable(false);
						Button(this["_btnChips" + str1]).addEventListener(MouseEvent.CLICK, setPourChipsText);
					}
				}
			}else 
			{
				for each(var str2:String in _connection)
				{
					Button(this["_btnChips" + str2]).removeEventListener(MouseEvent.CLICK, setPourChipsText);
				}
				
				hideAssest();
			}

			setBarDrag(status);
			
			switch(act)
			{
				case 'basePour':
					if (status)
					{
						_btnBasePour.show();
						_txtPour = _btnBasePour;
						
						_btnCallPour.hide();
						
						_btnBasePour.addEventListener(MouseEvent.CLICK, onBasePourClick);
						_btnGiveup.addEventListener(MouseEvent.CLICK, onBasePourGiveupClick);
					}else
					{
						_btnBasePour.removeEventListener(MouseEvent.CLICK, onBasePourClick);
						_btnGiveup.removeEventListener(MouseEvent.CLICK, onBasePourGiveupClick);
						
						_minPour = 0;
					}
					
					break;
					
				case 'callPour':
					if (status)
					{
						_btnCallPour.show();
						_txtPour = _btnCallPour;
						_btnBasePour.hide();
						
						_btnCallPour.addEventListener(MouseEvent.CLICK, onPourClick);
						_btnGiveup.addEventListener(MouseEvent.CLICK, onPlayrGiveupClick);
					}else
					{
						_btnCallPour.removeEventListener(MouseEvent.CLICK, onPourClick);
						_btnGiveup.removeEventListener(MouseEvent.CLICK, onPlayrGiveupClick);
					}
					
					break;
				
				case 'init':
				default:
					_btnBasePour.removeEventListener(MouseEvent.CLICK, onBasePourClick);
					_btnCallPour.removeEventListener(MouseEvent.CLICK, onPourClick);
					_btnGiveup.removeEventListener(MouseEvent.CLICK, onPlayrGiveupClick);
					
					_btnBasePour.setLabel("0");
					_btnCallPour.setLabel("0");
					break;
			}
			
			if (minPour >= 0)
			{
				_minPour = minPour;
				//_bar.x = (_minPour / _maxPour) * _scrollBarWidth;
				_txtPour.setLabel(String(_minPour));
			}
		}
		
		private function labelInputHandle(e:TextEvent):void
		{
			trace(e);
		}
		
		private function setPourChipsText(e:MouseEvent):void
		{
			var n:Array = e.target.assest.name.split("_");
			var chip:int = 0;
			if (_isHeapChips)
			{
				chip = int(_txtPour.getLabel()) + int(n[2]);
			}else
			{
				chip = int(n[2]);
			}
			
			if (chip < _minPour) chip = _minPour;
			if(chip > _maxPour) chip = _maxPour;
			
			_txtPour.setLabel(String(chip));
			
			setBarPosition(chip);
			_isHeapChips = true;
		}
		
		private function onBasePourClick(e:MouseEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_BASE_POUR);
			evt.seatid = _seatid;
			evt.state = 1;
			evt.message = _btnBasePour.getLabel();
			dispatchEvent(evt);
			evt = null;
		}
		
		private function onBasePourGiveupClick(e:MouseEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_BASE_POUR);
			evt.seatid = _seatid;
			evt.state = 0;
			evt.message = '0';
			dispatchEvent(evt);
			evt = null;
		}
		
		private function onPourClick(e:MouseEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_POUR);
			evt.seatid = _seatid;
			evt.state = 1;
			evt.message = _btnCallPour.getLabel();
			dispatchEvent(evt);
			evt = null;
		}
		
		private function onPlayrGiveupClick(e:MouseEvent):void
		{
			var evt:SeatEvent = new SeatEvent(SeatEvent.SEAT_POUR);
			evt.seatid = _seatid;
			evt.state = 0;
			evt.message = '0';
			dispatchEvent(evt);
			evt = null;
		}
	}

}